/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package common;

import client.Client;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.UnknownHostException;
import java.util.Random;
import server.Server;

/**
 *
 * @author windsea
 */
public class TeamViewerControl {

    private TeamViewer view;
    private String PORT;
    private String IP;

    public TeamViewerControl(TeamViewer view) {
        this.view = view;
        this.view.addConnectListener(new ConnectListener());
        this.view.addStartListener(new StartListener());
        this.view.addRefreshListener(new RefreshListener());
        this.view.addExitListener(new ExitListener());
        IP = getIP();
        PORT = getPortFree();
        view.setPort(PORT);
        view.setIP(IP);
        Server sv = new Server(Integer.parseInt(PORT));
        sv.startS();
    }

    public int randomserverport() {
        int port = 0;
        do {
            port = 0;
            Random number = new Random();
            for (int i = 0; i < 4; i++) {
                int x = number.nextInt();
                if (x < 0) {
                    x = -x;
                }
                port = port * 10 + x % 10;
            }
        } while (port < 1000);
        return port;

    }

    void refreshPort() {
        PORT = getPortFree();
        view.setPort(PORT);

    }

    private String getPortFree() {
        int i = randomserverport();
        try {
            ServerSocket ss = new ServerSocket(i);
            ss.close();
            return String.valueOf(i);
        } catch (IOException e) {
            view.showMessage("This port is unavailable");
        }

        return "-1";
    }

    private String getIP() {
        try {
            InetAddress local = InetAddress.getLocalHost();
            return local.getHostAddress();
        } catch (UnknownHostException e) {
            return "127.0.0.1";
        }
    }

    private void connectToServer() {
        new Client().startC(view.getPIP(), view.getPPort());
    }

    private class StartListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            //view.showCloseMessage();
            //Thread thread = new Thread(new Server(Integer.parseInt(PORT)).startC());
        }
    }

    private class RefreshListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            refreshPort();
            System.out.println("Port: " + PORT);
        }
    }

    private class ConnectListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            connectToServer();
        }
    }

    private class ExitListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }
}
